package com.dimples.security.handler;

import com.dimples.common.constant.DimplesConst;
import com.dimples.common.enums.CodeMsgEnum;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 认证失效处理器
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/27
 */
@Configuration
public class DimplesAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType(DimplesConst.JSON_UTF8);
        response.setStatus(CodeMsgEnum.LOGIN_SUCCESS.getCode());
        JSONObject result = new JSONObject();
        result.set("data", CollUtil.newArrayList());
        result.set("code", CodeMsgEnum.AUTH_DISABLE.getCode());
        result.set("msg", CodeMsgEnum.AUTH_DISABLE.getMessage());
        response.getWriter().write(JSONUtil.toJsonStr(result));
    }
}
